<?php

/**
 * 模型对应数据库表 "product_category".
 *
 * 以下是可用表product_category的栏位:
 * @property integer $ID
 * @property integer $parentid
 * @property string $title
 * @property integer $state
 * @property string $titlenote
 */
class ProductCategory extends CActiveRecord {
    const SHOW_SELECT = '————请选择————';
    const SHOW_TOPCATGORY = '————顶级分类————';
    const SHOW_ALLCATGORY = '————查看全部分类————';

    public $state_array = array(
        '0' => '禁用',
        '1' => '激活'
    );

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return ProductCategory the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'product_category';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('title', 'required'),
            array('sort,parentid, state', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 100),
            array('titlenote', 'length', 'max' => 225),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, parentid, title, state, titlenote', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '编号',
            'parentid' => '上级目录',
            'title' => '标题',
            'state' => '状态',
            'titlenote' => '简称',
            'sort' => '序号',
            'price' => '金币单价'
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('parentid', $this->parentid);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('state', $this->state);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 'title ASC'
            ),
            'pagination'=>array(
                'pageSize' => '10000'
            )
        ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取所有下级目录
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function showAllSelectCategory($selectText = '') {
        $cdb = new CDbCriteria;
        $cdb->select = "id,title,parentid";
        $cdb->order = "title ASC";
        $cdb->compare('state', 1);
        $cdb->compare('parentid', 0);
        $category = $this->findAll($cdb);

        foreach ($this->findAll($cdb) as $v) {
            $category = array_merge($category, $this->findAllByAttributes(array('parentid' => $v->id)));
        }

        $categoryList = array();
        $this->showAllCategory($categoryList, $category);
        if (!empty($selectText))
            $categorys = array('0' => $selectText);
        else
            $categorys = array('' => ProductCategory::SHOW_SELECT);
        foreach ($categoryList as $v) {
            $categorys[$v[id]] = $v[title];
        }
        return $categorys;
    }

    /**
     * 级联显示
     * 分类目录递归列表
     * 返回父类的所有子类
     * &$categoryList 引用传值返回
     */
    public function showAllCategory(&$categoryList, $category, $parentid = 0, $separate = "") {
        foreach ($category as $k => $v) {
            if ($v['parentid'] == $parentid) {
                $v['title'] = $separate . $v[title];
                $categoryList[] = $v;
                $this->showAllCategory($categoryList, $category, $v['id'], $separate . "——");
            }
        }
    }

    /**
      +-----------------------------------------------------
     * 说明 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getCategoryTitle($cObj) {
        $id = $cObj->id;
        $rStirng = "<span class='title_catagory fl'>┗</span><span class='fl' style='margin-right:20px;'><input size='30' type='text' value='" . $cObj->title . "' field='title' rel='" . $cObj->id . "' onChange='saveChange(this)'></input></span>";
        //存在下级，需要展示下级的按钮
        $vUrl = "#";
        if ($this->IsHaveLowerLevel($id)) {
            $rStirng.= '<a title="展开"  rel="' . $cObj->id . '" href="javaScript:void(0)" onclick="dropLowerLevel(this)" getdata="0" class="gradient-btn" >展开</a>';
        }
        $rStirng.= '<a title="复制" rel="' . $cObj->id . '" parent="1" href="javaScript:void(0)" onclick="copyCategory(this)" class="gradient-btn">复制</a>';
        $rStirng.= '<a title="添加" rel="' . $cObj->id . '" parent="1" href="javaScript:void(0)" onclick="addCategory(this)" class="gradient-btn">添加</a>';
        $rStirng.= '<a title="删除" parent="1"  rel="' . $cObj->id . '"  href="javaScript:void(0)" onclick="deleteCategory(this)" class="gradient-btn">删除</a>';
        return $rStirng;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取目录是否有下级 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function IsHaveLowerLevel($cid) {
        $data = $this->find("parentid='" . $cid . "'");
        return empty($data) ? false : true;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取表单
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getTextString($content, $id, $value = '', $style = array()) {
        $rstring = "";
        if (is_array($content)) {
            $rstring = CHtml::dropDownList('change_' . $id, $value, $content, $style);
        } else {
            $rstring = CHtml::textField('blur_' . $id, $content, $style);
        }
        return $rstring;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取游戏选择
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getGameSelect() {
        $rArray = array();
        $cdbc = new CDbCriteria;
        $cdbc->compare('state', 1);
        $cdbc->compare('parentid', 0);
        $cdbc->order = "title ASC";
        $data = $this->findAll($cdbc);
        foreach ($data as $sdata) {
            $cData = $this->findAll("parentid='" . $sdata->id . "'");
            $carray = array();
            foreach ($cData as $scdata) {
                $carray[$scdata->id] = $sdata->title . " - " . $scdata->title;
            }
            $rArray[$sdata->title] = $carray;
        }
        return $rArray;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取所有游戏选择
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getAllGameSelect() {
        $rArray = array();
        $cdbc = new CDbCriteria;
        $cdbc->compare('state', 1);
        $cdbc->compare('parentid', 0);
        $cdbc->order = "title ASC";
        $data = $this->findAll($cdbc);
        foreach ($data as $sdata) {
            $rArray[$sdata->id] = $sdata->title;
            $cData = $this->findAll("parentid='" . $sdata->id . "'");
            foreach ($cData as $scdata) {
                $rArray[$scdata->id] = "|--" . $sdata->title . " - " . $scdata->title;
            }
        }
        return $rArray;
    }

    /**
      +-----------------------------------------------------
     * 说明获取游戏一级目录 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getParentGame() {
        $rArray = array();
        $cr = new CDbCriteria;
        $cr->compare('state', 1);
        $cr->compare('parentid', 0);
        $cr->order = 'title ASC';
        $data = $this->findAll($cr);
        foreach ($data as $sdata) {
            $rArray[$sdata->id] = $sdata->title;
        }
        return $rArray;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取游戏名称
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getGameNameById($id) {
        $tModel = $this->findByPk($id);
        if ($tModel->parentid > 0) {
            $pModel = $this->findByPk($tModel->parentid);
            return $pModel->title . " - " . $tModel->title;
        } else {
            return $tModel->title;
        }
    }
    
    /**
     * 获取游戏服务区
     * @param type $game_id
     * @return type
     */
    public function getGameServers($game_id){
        $cr = new CDbCriteria;
        $cr->compare('parentid', $game_id);
        $cr->order = 'title';
        return $this->findAll($cr);
    }
    
    /**
     * Get Game server by name
     * @param type $name
     * @param type $game_id
     * @return type
     */
    public function getServerByName($name, $game_id){
        $cr = new CDbCriteria;
        $cr->compare('parentid', $game_id);
        $cr->compare('title', $name);
        return $this->find($cr);
    }

}
